package lagou.homework.lagouhomework.Unit3.code.q2;

public class MaxString {

    public static String  getMaxSonString(String s1, String s2){
        String maxString;
        String minString;
        String maxSonString="";
        String midSonString="";
        if (s1.length()<s2.length()) {
            minString = s1;
            maxString = s2;
        }else{
            minString =s2;
            maxString = s1;
        }
        b1:for(int i =minString.length();i>0;i--){          // 从最大长度的子串开始找
            for(int j =0;j<=minString.length()-i;j++)     // 从第一位开始，最后一个长度起始位是
            {
                //System.out.println(minString.substring(j, i+j+1));
                midSonString=minString.substring(j, i+j);         // 从第一位开始，截取长度为i的子串
                if(maxString.contains(midSonString)){
                    maxSonString=midSonString;
                    // System.out.println(maxSonString);
                    break b1;                                   // 取到即跳出循环，无需进行查更短长度的子串
                }
               // System.out.println(midSonString);

            }
        }

        return  maxSonString;
    }

    public static void main(String[] args) {
        String s1 = "asdafghjka";
        String s2 = "aaasdfg";
        System.out.println("字符串"+s1+"与字符串"+s2+"的最大子串是" + getMaxSonString(s1,s2));

    }

}
